<template>
  <div class="oe-update-pwd">
    <div class="oe-update-head">
      <img :src="avatarUrl || headimg" />
    </div>
    <div class="oe-update-from">
      <a-form-model
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        v-bind="layout"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item has-feedback label="旧密码" prop="oldPassword">
          <a-input-password v-model="ruleForm.oldPassword" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item has-feedback label="新密码" prop="newPassword">
          <a-input-password v-model="ruleForm.newPassword" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item has-feedback label="确认密码" prop="ascertainPassword">
          <a-input-password v-model="ruleForm.ascertainPassword" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item style="margin: -30px 0px 0px 97px;">
          <span
            class="iconfont icon-wenti o-exam-tips"
          >密码由大写字母、小写字母、数字、特殊字符中任意三种组成，长度为 8-32 位 ，特殊字符为：!@#$%^&*()_+-=等</span>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="submitForm('ruleForm')">确认修改</a-button>
          <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">清空</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </div>
</template>
<script>
import router from '../../../router';
export default {
  data() {
    return {
      headimg: 'https://iconfont.alicdn.com/t/1566197395754.jpg@100h_100w.jpg',
      avatarUrl: localStorage.getItem('avatorUrl'),
      ruleForm: {
        oldPassword: '',
        newPassword: '',
        ascertainPassword: ''
      },
      rules: {
        oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: this.password, message: '密码长度不小于8位，包含数字、字母大、小写、符号中的至少3种', trigger: 'blur' }
        ],
        ascertainPassword: [
          { required: true, message: '再次输入新密码', trigger: 'blur' },
          { validator: this.passwords, message: '俩次密码输入不一致', trigger: 'blur' }
        ]
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 18 }
    };
  },
  methods: {
    password() {
      var regUpper = /[A-Z]/;
      var regLower = /[a-z]/;
      var regNum = /[0-9]/;
      var regTeShu = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？+-]");
      var complex = 0;
      if (regLower.test(this.ruleForm.newPassword)) {
        ++complex;
      }
      if (regUpper.test(this.ruleForm.newPassword)) {
        ++complex;
      }
      if (regNum.test(this.ruleForm.newPassword)) {
        ++complex;
      }
      if (regTeShu.test(this.ruleForm.newPassword)) {
        ++complex;
      }
      if (complex < 3 || this.ruleForm.newPassword.length < 8 || this.ruleForm.newPassword.length > 32) {
        return false;
      } else {
        return true;
      }
    },
    passwords() {
      return this.ruleForm.newPassword == this.ruleForm.ascertainPassword;
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.editPassWord();
        } else {
          return false;
        }
      });
    },
    editPassWord() {
      this.$http
        .get('/api/common/accounts/editPassWord', {
          params: {
            Id: localStorage.getItem('userId'), //用户id
            oldPassword: this.ruleForm.oldPassword, //原密码
            newPassword: this.ruleForm.ascertainPassword //新密码
          }
        })
        .then((res) => {
          if (res.code == 1) {
            this.$message.success(res.msg);
            this.ruleForm.oldPassword = '';
            this.ruleForm.newPassword = '';
            this.ruleForm.ascertainPassword = '';
            this.$router.push({
              name: 'login'
            });
          } else if (res.code <= 0) {
            this.$message.error(res.msg);
          }
        });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  }
};
</script>
